package goods;

import java.util.ArrayList;
import java.util.List;

public class GoodsDaoImpl implements GoodsDao {
	// 1. 创建被管理的列表
	private List <Goods> goodList = new ArrayList <> ();
	
	@Override
	public boolean insert(Goods good) {
		// 判断此商品是否存在 
		if(goodList.contains(good)) {
			return false;
		}else {
			return goodList.add(good);
		}
	}

	@Override
	public boolean deleteById(int id) {
		// 封装成对象
		Goods g = new Goods();
		g.setId(id);
		// 删除此对象
		return goodList.remove(g);
	}

	@Override
	public boolean update(Goods good) {
		// 1.找到下表
		int index = goodList.indexOf(good);
		if (index != -1) {
			// 替換
			goodList.set(index, good);
			return true;
		}else {
		   return false;
		}
	}

	@Override
	public Goods selectById(int id) {
		// 封裝
		Goods g = new Goods();
		g.setId(id);
		// 找下標
		int index = goodList.indexOf(g);
		// 沒找到返回null, 找到返回此下標商品
		return index == -1 ? null : goodList.get(index);
	}

	@Override
	public List<Goods> selectAll() {
		return goodList;
	}
}
